package com.zatech.cgnci.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zatech.cgnci.project.base.utils.BeanUtils;
import com.zatech.cgnci.project.base.utils.ConvertUtils;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.entity.CgnciAccessoryDO;
import com.zatech.cgnci.project.entity.CgnciDirectInsuranceDO;
import com.zatech.cgnci.project.entity.CgnciDirectInsuranceDetailDO;
import com.zatech.cgnci.project.entity.CgnciDirectInsuranceRiskDO;
import com.zatech.cgnci.project.mapper.CgnciDirectInsuranceMapper;
import com.zatech.cgnci.project.service.CgnciAccessoryService;
import com.zatech.cgnci.project.service.CgnciDirectInsuranceDetailService;
import com.zatech.cgnci.project.service.CgnciDirectInsuranceRiskService;
import com.zatech.cgnci.project.service.CgnciDirectInsuranceService;
import com.zatech.cgnci.project.vo.CgnciAccessoryVO;
import com.zatech.cgnci.project.vo.CgnciDirectInsuranceDetailVO;
import com.zatech.cgnci.project.vo.CgnciDirectInsuranceRiskVO;
import com.zatech.cgnci.project.vo.CgnciDirectInsuranceVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 直保信息表 服务实现类
 * </p>
 *
 * @author Generator
 * @since 2024-12-17
 */
@Service
public class CgnciDirectInsuranceServiceImpl extends ServiceImpl<CgnciDirectInsuranceMapper, CgnciDirectInsuranceDO> implements CgnciDirectInsuranceService {

    @Resource
    private CgnciDirectInsuranceRiskService cgnciDirectInsuranceRiskService;

    @Resource
    private CgnciDirectInsuranceDetailService cgnciDirectInsuranceDetailService;

    @Resource
    private CgnciAccessoryService cgnciAccessoryService;

    @Override
    public CgnciDirectInsuranceVO getDirectInsuranceByBusinessId(Long businessId) {
        CgnciDirectInsuranceDO directInsuranceDO = this.getOne(new LambdaQueryWrapper<CgnciDirectInsuranceDO>()
                .eq(CgnciDirectInsuranceDO::getBusinessId, businessId));
        if (VerifyUtils.isNotEmpty(directInsuranceDO)) {
            CgnciDirectInsuranceVO directInsuranceVO = ConvertUtils.convert(directInsuranceDO, CgnciDirectInsuranceVO.class);
            // 直保险种信息
            List<CgnciDirectInsuranceRiskDO> directInsuranceRiskList = cgnciDirectInsuranceRiskService.list(new LambdaQueryWrapper<CgnciDirectInsuranceRiskDO>()
                    .eq(CgnciDirectInsuranceRiskDO::getDirectInsuranceId, directInsuranceDO.getDirectId()));
            if (VerifyUtils.isNotEmpty(directInsuranceRiskList)) {
                List<CgnciDirectInsuranceRiskVO> directInsuranceRiskVOList = ConvertUtils.convert(directInsuranceRiskList, CgnciDirectInsuranceRiskVO.class);
                // 直保险种ID集合
                List<Long> riskIds = directInsuranceRiskVOList.stream().map(CgnciDirectInsuranceRiskVO::getRiskId).collect(Collectors.toList());

                Map<Long, List<CgnciDirectInsuranceDetailVO>> directIdMap = new HashMap<>();
                Map<Long, List<CgnciAccessoryVO>> detailIdMap = new HashMap<>();


                // 根据直保险种ID查询直保险种标的信息
                List<CgnciDirectInsuranceDetailDO> detailDOList = cgnciDirectInsuranceDetailService.list(new LambdaQueryWrapper<CgnciDirectInsuranceDetailDO>()
                        .in(CgnciDirectInsuranceDetailDO::getDirectId, riskIds));
                if (VerifyUtils.isNotEmpty(detailDOList)) {
                    List<CgnciDirectInsuranceDetailVO> detailVOList = ConvertUtils.convert(detailDOList, CgnciDirectInsuranceDetailVO.class);
                    // 直保险种标的ID集合
                    List<Long> detailIds = detailVOList.stream().map(CgnciDirectInsuranceDetailVO::getDetailId).collect(Collectors.toList());
                    // 直保险种ID和直保险种标的信息映射
                    directIdMap = detailVOList.stream().collect(Collectors.groupingBy(CgnciDirectInsuranceDetailVO::getDirectId));
                    // 根据直保险种标的ID查询附件信息
                    List<CgnciAccessoryDO> accessoryDOList = cgnciAccessoryService.list(new LambdaQueryWrapper<CgnciAccessoryDO>()
                            .in(CgnciAccessoryDO::getBusinessId, detailIds));
                    if (VerifyUtils.isNotEmpty(accessoryDOList)) {
                        List<CgnciAccessoryVO> accessoryVOList = ConvertUtils.convert(accessoryDOList, CgnciAccessoryVO.class);
                        // 直保险种标的ID和附件信息映射
                        detailIdMap = accessoryVOList.stream().collect(Collectors.groupingBy(CgnciAccessoryVO::getBusinessId));
                    }
                }


                // 遍历直保险种信息，设置直保险种标的信息
                Map<Long, List<CgnciDirectInsuranceDetailVO>> finalDirectIdMap = directIdMap;
                Map<Long, List<CgnciAccessoryVO>> finalDetailIdMap = detailIdMap;
                directInsuranceRiskVOList.forEach(risk -> {
                    if(VerifyUtils.isNotEmpty(finalDirectIdMap)){
                        // 根据直保险种ID获取直保险种标的信息
                        List<CgnciDirectInsuranceDetailVO> detailVOS = finalDirectIdMap.get(risk.getRiskId());
                        if (VerifyUtils.isNotEmpty(detailVOS)) {
                            detailVOS.forEach(riskDetailVO -> {
                                if(VerifyUtils.isNotEmpty(finalDetailIdMap)){
                                    riskDetailVO.setAccessoryVOList(finalDetailIdMap.get(riskDetailVO.getDetailId()));
                                }
                            });
                        }
                        risk.setDetails(detailVOS);
                    }
                });
                directInsuranceVO.setInsuranceRisks(directInsuranceRiskVOList);
            }
            return directInsuranceVO;
        }
        return null;
    }

    @Override
    public void updateDirectInsuranceColumn(CgnciDirectInsuranceRiskDO insuranceRiskDO, CgnciDirectInsuranceDO directInsuranceDO, Long detailId) {
        // 根据直保险种id和直保标的id查询直保信息
        List<CgnciDirectInsuranceDetailDO> detailDOS = cgnciDirectInsuranceDetailService.list(new LambdaQueryWrapper<CgnciDirectInsuranceDetailDO>()
                .eq(CgnciDirectInsuranceDetailDO::getDirectId, insuranceRiskDO.getRiskId())
                .eq(VerifyUtils.isNotEmpty(detailId), CgnciDirectInsuranceDetailDO::getDetailId, detailId));

        // 保费（含税）
        if (VerifyUtils.isNotEmpty(directInsuranceDO.getPremiumInclTax())) {
            // 汇总直保标的保费（含税）
            BigDecimal premiumInclTax = BeanUtils.sumBigDecimalProperty(detailDOS, CgnciDirectInsuranceDetailDO::getPremiumInclTax);
            directInsuranceDO.setPremiumInclTax(directInsuranceDO.getPremiumInclTax().subtract(premiumInclTax));
        }

        // 保费（不含税）
        if (VerifyUtils.isNotEmpty(directInsuranceDO.getPremiumExclTax())) {
            // 汇总直保标的保费（不含税）
            BigDecimal premiumExclTax = BeanUtils.sumBigDecimalProperty(detailDOS, CgnciDirectInsuranceDetailDO::getPremiumExclTax);
            directInsuranceDO.setPremiumExclTax(directInsuranceDO.getPremiumExclTax().subtract(premiumExclTax));
        }
        this.updateById(directInsuranceDO);
    }
}
